﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Indigo.Quibbler.Controls
{
    public partial class SubCategoryControl : UserControl
    {
        public List<string> ItemSource
        {
            get { return (List<string>)GetValue(ItemSourceProperty); }
            set { SetValue(ItemSourceProperty, value); }
        }

        public int SelectedIndex
        {
            get { return (int)GetValue(SelectedIndexProperty); }
            set { SetValue(SelectedIndexProperty, value); }
        }

        public static readonly DependencyProperty SelectedIndexProperty =
            DependencyProperty.Register("SelectedIndex", typeof(int), typeof(SubCategoryControl), new UIPropertyMetadata(0, OnSelectedIndexChanged));

        public static readonly DependencyProperty ItemSourceProperty =
           DependencyProperty.Register("ItemSource", typeof(List<string>), typeof(SubCategoryControl), new UIPropertyMetadata(null, OnItemSourceChanged));

        public event CategorySelectionEventHandler SelectionChanged;

        public delegate void CategorySelectionEventHandler(object sender, string category);

        public SubCategoryControl()
        {
            this.InitializeComponent();
            listBox.SelectionChanged += new SelectionChangedEventHandler(_onListSelectionChanged);
        }

        void _onListSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                this.SelectedIndex = listBox.SelectedIndex;
                if (this.SelectedIndex != -1)
                {
                    if (SelectionChanged != null)
                    {
                        SelectionChanged(this, this.ItemSource[SelectedIndex]);
                    }
                }
            }
            catch { }
        }

        private static void OnItemSourceChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            (obj as SubCategoryControl).OnItemSourceChanged(e);
        }

        private static void OnSelectedIndexChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            (obj as SubCategoryControl).OnSelectedIndexChanged(e);
        }

        private void OnSelectedIndexChanged(DependencyPropertyChangedEventArgs e)
        {
            try
            {
                if ((int)e.NewValue >= 0 && (int)e.NewValue <= ItemSource.Count - 1)
                    listBox.SelectedIndex = (int)e.NewValue;
                //else
                //    listBox.SelectedIndex = 0;
            }
            catch { }
        }

        private void OnItemSourceChanged(DependencyPropertyChangedEventArgs e)
        {
            try
            {
                listBox.ItemsSource = (List<string>)e.NewValue;
                if (listBox.ItemsSource != null)
                {
                    if (SelectedIndex >= 0 && SelectedIndex <= ItemSource.Count - 1)
                        listBox.SelectedIndex = SelectedIndex;
                    else
                        listBox.SelectedIndex = -1;
                }
                else
                {
                    listBox.SelectedIndex = -1;
                }
            }
            catch { }
        }
    }
}